using System;

using OST.Shared.SharedTypes;

using mfLogger;

namespace OST.Calc.Tracking.Extensions
{
    /// <summary>
    /// contains extension-methods for <see cref="TrackingDay"/>
    /// </summary>
    public static class TrackingDayExtensions
    {
        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private static ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        /// <summary>
        /// prints the specified tracking-day to the logger on Debug-Level
        /// </summary>
        /// <param name="day">tracking-day</param>
        public static void Print2DebugLog(this TrackingDay day)
        {
            Logger.Debug("Day=" + day + ", with " + day.Action.Count + " actions");

            foreach (var action in day.Action)
            {
                Logger.Debug(action.ToString());
            }
        }

        /// <summary>
        /// tries to find the initial <see cref="TrackingAction"/> on specified <see cref="TrackingDay"/>
        /// </summary>
        /// <param name="day">tracking-day</param>
        /// <returns>the index of the initial <see cref="TrackingAction"/></returns>
        public static int Try2FindInitialActionIndex(this TrackingDay day)
        {
            Logger.Debug("try 2 find initial action");

            if (day.Action == null || (day.Action != null && day.Action.Count == 0))
            {
                Logger.Error("actions are empty");
            }

            var currentActionIndex = -1;
            var now = DateTime.Now;
            //calculate initial action-index
            for (int i = 1; i < day.Action.Count; i++)
            {
                if (day.Action[(i - 1)].Sunposition.Timestamp < now
                    && now < day.Action[i].Sunposition.Timestamp)
                {
                    currentActionIndex = (i == 0) ? 0 : (i - 1);
                    Logger.Debug("initial action found " + day.Action[currentActionIndex]);
                    break;
                }
            }
            if (currentActionIndex == -1)
            {
                currentActionIndex = day.Action.Count - 1;
                Logger.Warning("Try2FindInitialActionIndex(): no action-index found, last one ist used");
            }

            return currentActionIndex;
        }

        /// <summary>
        /// determines the next index on the specified <see cref="TrackingDay"/>
        /// </summary>
        /// <param name="day">specified tracking-day</param>
        /// <param name="lastActionIndex">last actionIndex</param>
        /// <returns>the next <see cref="TrackingAction"/>-index</returns>
        public static int NextActionIndex(this TrackingDay day, int lastActionIndex)
        {
            if ((lastActionIndex + 1) >= day.Action.Count
                || lastActionIndex < -1)
            {
                Logger.Warning("NextActionIndex(): lastActionIndex overdrive");
                return lastActionIndex;
            }
            else
                return lastActionIndex + 1;
        }

        /// <summary>
        /// access the actions on <see cref="TrackingDay"/>, with the specified index
        /// </summary>
        /// <param name="day">the specified tracking-day</param>
        /// <param name="actionIndex">target action-index</param>
        /// <returns>the target <see cref="TrackingAction"/></returns>
        public static TrackingAction GetAction(this TrackingDay day, int actionIndex)
        {
            return day.Action[actionIndex];
        }
    }
}
